package week.Aug0803;

/**
 * 题目：3638. 平衡装运的最大数量
 * 链接：https://leetcode.cn/problems/maximum-balanced-shipments/description/
 * 思路：动态规划，求前缀，找到当前之前最大的，跟当前比，大于，就存为最大值，
 * 【i-1】小于，则符合条件，更新dp[i]
 */
public class MaxBalancedShipments {
    public static void main(String[] args) {
        System.out.println(maxBalancedShipments(new int[]{2,5,1,4,3}));
    }
    public static int maxBalancedShipments(int[] weight) {
        int n = weight.length;
        if(n==0) return 0;
        if(n==2) return weight[0] > weight[1] ? 1 : 0;
        // 动态规划
        int[] dp = new int[n + 1];
        dp[0] = 0;
        for (int i = 1; i <= n; i++) {
            int max = weight[i - 1];
            for (int j = i - 1; j >= 0; j--) {
                if (weight[j] > max) {
                    max = weight[j];
                }
                if (weight[i - 1] < max) {
                    dp[i] = Math.max(dp[i], (j == 0 ? 0 : dp[j]) + 1);
                }
            }
        }
        return dp[n];
    }
    public static int maxBalancedShipments2(int[] weight) {
        int n = weight.length;
        if(n==0) return 0;
        int[] dp = new int[n + 1];
        dp[0] = 0;
        for (int i = 1; i <= n; i++) {
            dp[i] = dp[i - 1];
            int max = weight[i - 1];
            for (int j = i - 1; j >= 0; j--) {
                if(weight[j] > max) {
                    max = weight[j];
                }
                if (weight[i - 1] < max) {
                    dp[i] = Math.max(dp[i], (j == 0 ? 0 : dp[j]) + 1);
                }
            }
        }
        return dp[n];
    }
    public static boolean balancedShipment(int[] weight,int start,int end) {
        int max = Integer.MIN_VALUE;
        int min = Integer.MAX_VALUE;
        // 记录最大的值和最小值
        for (int i = start; i <= end; i++) {
            max = Math.max(max, weight[i]);
            min = Math.min(min, weight[i]);
        }
        return max > min;
    }
}
